package com.sojson.util.pay.wx.bean;

import com.ijpay.core.kit.PayKit;
import com.ijpay.core.kit.WxPayKit;
import com.sojson.result.bean.Status;
import com.sojson.util.pay.bean.PayResult;
import com.sojson.util.pay.wx.constant.WxConstant;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 返回给前端的微信接口参数Bean
 * 
 * @author liu
 * @date 2021-03-16
 */
@ApiModel("微信接口返回信息")
public class WxResult extends PayResult {

    /***/
    private static final long serialVersionUID = 2231406852564862492L;

    @ApiModelProperty("appid类似帐号")
    private String appid;
    @ApiModelProperty("网页授权接口调用凭证,注意：此access_token与基础支持的access_token不同")
    private String accessToken;
    @ApiModelProperty("用户唯一标识，请注意，在未关注公众号时，用户访问公众号的网页，也会产生一个用户和公众号唯一的OpenID")
    private String openid;
    @ApiModelProperty("用户在开放平台的唯一标识符")
    private String unionid;
    @ApiModelProperty("商户号")
    private String mchid;
    @ApiModelProperty("会话密钥")
    private String sessionKey;
    @ApiModelProperty("二维码链接")
    private String codeUrl;
    @ApiModelProperty("支付跳转链接")
    private String wapUrl;
    @ApiModelProperty("预支付交易会话ID")
    private String prepayid;
    @ApiModelProperty("订单详情扩展字符串")
    private String descPackage;
    @ApiModelProperty("时间戳")
    private long timestamp;
    @ApiModelProperty("生成密钥的随机字符串")
    private String nonceStr;
    @ApiModelProperty("页面路径")
    private String url;
    @ApiModelProperty("签名类型")
    private String signType;
    @ApiModelProperty("签名")
    private String sign;
    @ApiModelProperty("jsapiTicket")
    private String jsapiTicket;
    @ApiModelProperty("状态信息")
    private Status status;

    public WxResult() {}

    /**
     * 成功返回
     * @param code
     */
    public WxResult(Integer code) {
        this(code, null);
    }

    /**
     * 返回失败
     * @param code
     * @param msg
     */
    public WxResult(Integer code, String msg) {
        this(new Status(code, msg));
    }

    public WxResult(Status status) {
        this.status = status;
    }

    public WxResult(String accessToken, String openid) {
        this.accessToken = accessToken;
        this.openid = openid;
    }

    /** 获取Appid */
    public String getAppid() {
        return appid;
    }

    /** 设置Appid */
    public void setAppid(String appid) {
        this.appid = appid;
    }

    /** 获取AccessToken */
    public String getAccessToken() {
        return accessToken;
    }

    /** 设置AccessToken */
    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    /** 获取用户唯一标识 */
    public String getOpenid() {
        return openid;
    }

    /** 设置用户唯一标识 */
    public void setOpenid(String openid) {
        this.openid = openid;
    }

    /** 获取用户在开放平台的唯一标识符 */
    public String getUnionid() {
        return unionid;
    }

    /** 设置用户在开放平台的唯一标识符 */
    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    /** 获取商户号 */
    public String getMchid() {
        return mchid;
    }

    /** 设置商户号 */
    public void setMchid(String mchid) {
        this.mchid = mchid;
    }

    /** 获取会话密钥 */
    public String getSessionKey() {
        return sessionKey;
    }

    /** 设置会话密钥 */
    public void setSessionKey(String sessionKey) {
        this.sessionKey = sessionKey;
    }

    /** 获取二维码链接 */
    public String getCodeUrl() {
        return codeUrl;
    }

    /** 设置二维码链接 */
    public void setCodeUrl(String codeUrl) {
        this.codeUrl = codeUrl;
    }

    /** 获取支付跳转链接 */
    public String getWapUrl() {
        return wapUrl;
    }

    /** 设置支付跳转链接 */
    public void setWapUrl(String wapUrl) {
        this.wapUrl = wapUrl;
    }

    /** 获取预支付交易会话ID */
    public String getPrepayid() {
        return prepayid;
    }

    /** 设置预支付交易会话ID */
    public void setPrepayid(String prepayid) {
        this.prepayid = prepayid;
    }

    /** 获取订单详情扩展字符串 */
    public String getDescPackage() {
        return descPackage;
    }

    /** 设置订单详情扩展字符串 */
    public void setDescPackage(String descPackage) {
        this.descPackage = descPackage;
    }

    /** 获取时间戳 */
    public long getTimestamp() {
        return timestamp;
    }

    /** 设置时间戳 */
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    /** 获取生成密钥的随机字符串 */
    public String getNonceStr() {
        return nonceStr;
    }

    /** 设置生成密钥的随机字符串 */
    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    /** 获取页面路径 */
    public String getUrl() {
        return url;
    }

    /** 设置页面路径 */
    public void setUrl(String url) {
        this.url = url;
    }

    /** 获取签名类型 */
    public String getSignType() {
        return signType;
    }

    /** 设置签名类型 */
    public void setSignType(String signType) {
        this.signType = signType;
    }

    /** 获取签名 */
    public String getSign() {
        return sign;
    }

    /** 设置签名 */
    public void setSign(String sign) {
        this.sign = sign;
    }

    /** 获取JsapiTicket */
    public String getJsapiTicket() {
        return jsapiTicket;
    }

    /** 设置JsapiTicket */
    public void setJsapiTicket(String jsapiTicket) {
        this.jsapiTicket = jsapiTicket;
    }

    /** 获取状态信息 */
    public Status getStatus() {
        return status;
    }

    /** 设置状态信息 */
    public void setStatus(Status status) {
        this.status = status;
    }

    /**
     * 初始化签名内容
     * @throws Exception 
     */
    public void initSign(String prepayId) throws Exception {
        String buildSignMessage = PayKit.buildSignMessage(getAppid(), getTimestamp(), getNonceStr(), prepayId);
        setSign(PayKit.createSign(buildSignMessage, WxConstant.CA_CERT_FILE_PATH_PEM_KEY));
    }

    /**
     * 初始化Native调起支付内容
     * 
     * @param wxResponseV3
     */
    public void initNative(WxResponseV3 wxResponseV3) {
        initNative(wxResponseV3.getCodeUrl());
    }

    /**
     * 初始化Native调起支付内容
     * 
     * @param codeUrl  二维码内容
     */
    public void initNative(String codeUrl) {
        setCodeUrl(codeUrl);
    }

    /**
     * 初始化App调起支付内容
     * 
     * @param wxResponseV3
     * @throws Exception 
     */
    public void initApp(WxResponseV3 wxResponseV3) throws Exception {
        initApp(wxResponseV3, wxResponseV3.getAppid(), wxResponseV3.getMchid());
    }

    /**
     * 初始化App调起支付内容
     * 
     * @param wxResponseV3
     * @param appid     Appid
     * @param mchid     商户号
     * @throws Exception 
     */
    public void initApp(WxResponseV3 wxResponseV3, String appid, String mchid) throws Exception {
        initApp(appid, mchid, wxResponseV3.getPrepayId());
    }

    /**
     * 初始化App调起支付内容
     * 
     * @param appid     Appid
     * @param mchid     商户号
     * @param prepayId  预支付会话标识
     * @throws Exception
     */
    public void initApp(String appid, String mchid, String prepayId) throws Exception {
        setAppid(appid);
        setMchid(mchid);
        setPrepayid(prepayId);
        setDescPackage(WxConstant.PACKAGE_SING_WXPAY);
        setNonceStr(WxPayKit.generateStr());
        setTimestamp(System.currentTimeMillis() / 1000);
        initSign(getPrepayid());
    }

    /**
     * 初始化小程序调起支付内容
     * 
     * @param wxResponseV3
     * @throws Exception 
     */
    public void initXcx(WxResponseV3 wxResponseV3) throws Exception {
        initXcx(wxResponseV3, wxResponseV3.getAppid());
    }

    /**
     * 初始化小程序调起支付内容
     * 
     * @param wxResponseV3
     * @param appid         Appid
     * @throws Exception 
     */
    public void initXcx(WxResponseV3 wxResponseV3, String appid) throws Exception {
        initXcx(wxResponseV3.getAppid(), wxResponseV3.getPrepayId());
    }

    /**
     * 初始化小程序调起支付内容
     * 
     * @param appid     Appid
     * @param prepayId  预支付会话标识
     * @throws Exception
     */
    public void initXcx(String appid, String prepayId) throws Exception {
        setAppid(appid);
        setTimestamp(System.currentTimeMillis() / 1000);
        setNonceStr(WxPayKit.generateStr());
        setDescPackage(String.format(WxConstant.PACKAGE_PERPAY_ID, prepayId));
        setSignType(WxConstant.SING_TYPE_RSA);
        initSign(getDescPackage());
    }

    /**
     * 初始化JSAPI调起支付内容
     * 
     * @param wxResponseV3
     * @throws Exception 
     */
    public void initJsapi(WxResponseV3 wxResponseV3) throws Exception {
        initJsapi(wxResponseV3, wxResponseV3.getAppid());
    }

    /**
     * 初始化JSAPI调起支付内容
     * 
     * @param wxResponseV3
     * @param appid         Appid
     * @throws Exception 
     */
    public void initJsapi(WxResponseV3 wxResponseV3, String appid) throws Exception {
        initJsapi(appid, wxResponseV3.getPrepayId());
    }

    /**
     * 初始化JSAPI调起支付内容
     * 
     * @param appid     Appid
     * @param prepayId  预支付会话标识
     * @throws Exception
     */
    public void initJsapi(String appid, String prepayId) throws Exception {
        setAppid(appid);
        setTimestamp(System.currentTimeMillis() / 1000);
        setNonceStr(WxPayKit.generateStr());
        setDescPackage(String.format(WxConstant.PACKAGE_PERPAY_ID, prepayId));
        setSignType(WxConstant.SING_TYPE_RSA);
        initSign(getDescPackage());
    }

    /**
     * 初始化Wap调起支付内容
     * 
     * @param wxResponseV3
     * @throws Exception 
     */
    public void initWap(WxResponseV3 wxResponseV3) throws Exception {
        initWap(wxResponseV3.getWapUrl());
    }

    /**
     * 初始化Wap调起支付内容
     * 
     * @param wapUrl    支付跳转链接
     * @throws Exception
     */
    public void initWap(String wapUrl) throws Exception {
        setWapUrl(wapUrl);
    }

}